package com.douwong.student.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.fragment.AnswerQuestionExchangeFragment;
import com.douwong.student.fragment.HandExchangeRecordFragment;
import com.douwong.student.view.CommSearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 答题记录
 *
 * @author Administrator
 */
public class AnswerRecordActivity extends BaseActivity {

    private ImageView go_back_img;
    public List<Map<String, Object>> mNavData;
    public Map<String, Object> mNavDataSelect;
    private LinearLayout answer_ll, handle_ll, job_ll, class_loaded_ll, delete_time_ll;
    private ImageView answer_img, handle_img, job_img, time_search;
    private TextView answer_text, handle_text, job_text, slect_time;
    private static final String mTAG = "AnswerRecordActivity";
    private LinearLayout prepare_all_ll;
    private CommSearch mCommSearch;
    private TextView mCurrentSubject;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, AnswerRecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        //与备课窗口共享一个layout文件
        setContentView(R.layout.activity_prepare);
        go_back_img = (ImageView) findViewById(R.id.go_back_img);
        answer_img = (ImageView) findViewById(R.id.class_share_img);
        handle_img = (ImageView) findViewById(R.id.class_cloud_img);
        job_img = (ImageView) findViewById(R.id.class_uplod_img);


        answer_text = (TextView) findViewById(R.id.class_share_text);
        handle_text = (TextView) findViewById(R.id.class_cloud_text);
        job_text = (TextView) findViewById(R.id.class_uplod_text);
        slect_time = (TextView) findViewById(R.id.select_time);
        delete_time_ll = (LinearLayout) findViewById(R.id.delete_time_ll);
        mCurrentSubject = (TextView) findViewById(R.id.classroom_chooseSubject);


        answer_ll = (LinearLayout) findViewById(R.id.class_share_ll);
        handle_ll = (LinearLayout) findViewById(R.id.class_cloud_ll);
        class_loaded_ll = (LinearLayout) findViewById(R.id.class_loaded_ll);
        time_search = (ImageView) findViewById(R.id.time_search);
        job_ll = (LinearLayout) findViewById(R.id.class_uplod_ll);
        prepare_all_ll = (LinearLayout) findViewById(R.id.prepare_all_ll);
    }

    @Override
    protected void initData() {
        super.initData();
        handle_img.setImageResource(R.mipmap.hand_2);
        job_img.setImageResource(R.mipmap.job_2);
        answer_text.setText(getResources().getString(R.string.answer));
        handle_text.setText(getResources().getString(R.string.hand));
        //功能隐藏了
        job_text.setText(getResources().getString(R.string.class_job));
        job_ll.setVisibility(View.GONE);

        findViewById(R.id.class_video_ll).setVisibility(View.GONE);
        class_loaded_ll.setVisibility(View.GONE);
        initListView();
        mCommSearch = new CommSearch(this);
        time_search.setVisibility(View.VISIBLE);
        prepare_all_ll.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ClassOnFocusListener onFoucsClik = new ClassOnFocusListener();
        AnswerOnClick clickListener = new AnswerOnClick();

        answer_ll.setOnFocusChangeListener(onFoucsClik);
        handle_ll.setOnFocusChangeListener(onFoucsClik);
        // job_ll.setOnFocusChangeListener(onFoucsClik);
        go_back_img.setOnClickListener(clickListener);
        time_search.setOnClickListener(clickListener);
        prepare_all_ll.setOnClickListener(clickListener);
        delete_time_ll.setOnClickListener(clickListener);

    }

    private class AnswerOnClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 返回
                case R.id.go_back_img:
                    finish();
                    break;
                case R.id.time_search:
                    //时间查询
                    mCommSearch.showTimeWindow(delete_time_ll, slect_time, CommonConfig.CLASS_RECORD_SERACH);

                    break;
                case R.id.prepare_all_ll:
                    //选择科目
                    mCommSearch.showSubject(mCurrentSubject, delete_time_ll, CommonConfig.CLASS_RECORD_SERACH);

                    break;
                case R.id.delete_time_ll:
                    delete_time_ll.setVisibility(View.GONE);
                    mCommSearch.setStartTime(null, CommonConfig.CLASS_RECORD_SERACH);
                    break;
            }
        }

    }

    // 选项切换判断

    private class ClassOnFocusListener implements OnFocusChangeListener {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            switch (v.getId()) {
                // 问题互动
                case R.id.class_share_ll:
                    if (hasFocus) {
                        answer_img.setImageResource(R.mipmap.answer_1);
                        answer_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
                        answer_text.setTextColor(0Xff7d7d7d);

                        showFragment(mNavData.get(0));
                    } else {
                        answer_img.setImageResource(R.mipmap.answer_2);
                        answer_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
                        answer_text.setTextColor(0Xffffffff);
                    }
                    break;
                // 手写互动
                case R.id.class_cloud_ll:
                    if (hasFocus) {
                        handle_img.setImageResource(R.mipmap.hand_1);
                        handle_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
                        handle_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(1));
                    } else {
                        handle_img.setImageResource(R.mipmap.hand_2);
                        handle_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
                        handle_text.setTextColor(0Xffffffff);
                    }
                    break;
                // 课堂任务
                case R.id.class_uplod_ll:
                    if (hasFocus) {
                        job_img.setImageResource(R.mipmap.job_1);
                        job_ll.setBackgroundColor(getResources().getColor(R.color.class_file_word));
                        job_text.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(2));
                    } else {
                        job_img.setImageResource(R.mipmap.job_2);
                        job_ll.setBackgroundColor(getResources().getColor(R.color.class_file_leftbackground));
                        job_text.setTextColor(0Xffffffff);
                    }
                    break;

            }
        }
    }

    /**
     * 显示内容
     *
     * @param map
     */
    public void showFragment(Map<String, Object> map) {
        mNavDataSelect = map;
       FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = (Fragment) map.get("fragment");
        boolean isInit = Boolean.parseBoolean(map.get("isInit").toString());

        // 检查是否存在
        if (!isInit) {
            transaction.add(R.id.frament, fragment);
            map.put("isInit", true);
            Log.d(mTAG, String.format("create new fragment[%s]:%s", map.get("name"), map.get("title")));
        }

        // 隐藏&显示控件
        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            if (itemFragment == null) {
                continue;
            }
            if (itemFragment.equals(fragment)) {
                transaction.show(itemFragment);
            } else {
                transaction.hide(itemFragment);
            }
        }
        transaction.commit();
    }

    private void initListView() {
        mNavData = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        map = new HashMap<String, Object>();
     //  map.put("fragment", new AnswerExchangeFragment());
        map.put("fragment", new AnswerQuestionExchangeFragment());
        map.put("isInit", true);
        mNavData.add(map);

        map = new HashMap<String, Object>();
     //   map.put("fragment", new HandExchangeFragment());
       map.put("fragment", new HandExchangeRecordFragment());

        map.put("isInit", true);
        mNavData.add(map);


        this.initFragment();
    }


    /**
     * 初始化Fragment
     */

    private void initFragment() {
       FragmentManager fragmentManager = getSupportFragmentManager();
       FragmentTransaction transaction = fragmentManager.beginTransaction();

        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            boolean isInit = Boolean.parseBoolean(item.get("isInit").toString());
            if (isInit && itemFragment != null) {
                transaction.add(R.id.frament, itemFragment);
                transaction.hide(itemFragment);
            }
        }

        transaction.commit();
    }
}
